/**
 * Copyright (c) Aspose 2002-2014. All Rights Reserved.
 *
 * LICENSE: This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not,
 * see http://opensource.org/licenses/gpl-3.0.html
 *
 * @author Adeel Ilyas <adeel.ilyas@aspose.com>
 *
 */

package com.aspose.examples;

import com.aspose.examples.examplesmodel.Example;
import com.aspose.examples.otherexamples.OtherExamples;
import com.aspose.examples.otherexamples.OtherExamplesManager;
import com.aspose.utils.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

public class AsposeExampleDialog extends DialogWrapper {
    private static final Logger LOG = Logger.getInstance("#com.aspose.examples.AsposeExampleDialog");

    private final String myDescription;
    private AsposeExamplePanel component;
    private Project project;

    public AsposeExampleDialog(final String title, String description, Project project) {
        super(false);
        myDescription = description;
        this.project = project;
        setTitle(title);
        init();
        setOKButtonText("Create");
        setOKActionEnabled(false);
    }

    public void updateControls(boolean selection) {
        setOKActionEnabled(selection);
    }

    @Override
    protected void doOKAction() {
        super.doOKAction();
        createExample();
        project.getProjectFile().getFileSystem().refresh(false);
        project.getBaseDir().getFileSystem().refresh(false);
      }

    @Override
    public JComponent getPreferredFocusedComponent() {
        AsposeConstants.println("AsposeExamplePanel getComponent(): is called ...");
        if (component == null) {
            component = new AsposeExamplePanel(this, project);
        }

        return component.getComponentSelection();
    }

    @Override
    protected JComponent createCenterPanel() {
        AsposeConstants.println("AsposeExamplePanel getComponent(): is called ...");
        if (component == null) {
            component = new AsposeExamplePanel(this, project);
        }
        return component;
    }

    @Override
    protected String getDimensionServiceKey() {
        return "#com.aspose.examples.AsposeExampleDialog";
    }

    //=========================================================================


    //=========================================================================
    private boolean createExample() {
        String projectPath = component.getSelectedProjectRootPath();
        CustomMutableTreeNode comp = getSelectedNode();
        if (comp == null) {
            return false;
        }
        try {
            String path = comp.getExPath();
            Example ex = comp.getExample();
            AsposeJavaComponent asposeComponent = AsposeJavaComponents.list.get(component.getComponentSelection().getSelectedItem());

            // Integration of Apache POI Examples / Other FrameWork Examples
            String sourceRepositoryPath = GitHelper.getLocalRepositoryPath(asposeComponent) + File.separator + path;
            String destinationPath = projectPath + File.separator + path;

            if (!asposeComponent.getOtherFrameworkExamples().isEmpty()) {
                for (OtherExamples _otherExample : asposeComponent.getOtherFrameworkExamples()) {
                    String examplesName = _otherExample.getExampleName();
                    if (destinationPath.contains(examplesName)) {
                        destinationPath = destinationPath.replace(examplesName + File.separator, "");
                        OtherExamplesManager.installExamplesDependencies(_otherExample, projectPath,project);
                        break;
                    }
                }
            }

            copyExample(sourceRepositoryPath, destinationPath);
            // END - Integration of Apache POI Examples / Other FrameWork Examples

            if (ex == null) {
                return false;
            }

        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    //=========================================================================
    private CustomMutableTreeNode getSelectedNode() {
        return (CustomMutableTreeNode) component.getExamplesTree().getLastSelectedPathComponent();
    }

    //=========================================================================
    private void copyExample(String sourcePath, String destinationPath) {
        try {
            AsposeComponentsManager.copyDirectory(sourcePath, destinationPath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
